package net.swa.system.util.interceptor;


import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.JsonResult;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ExceptionInterceptor extends AbstractInterceptor{
	private static final long serialVersionUID = -435231817099494168L;
	
	public String intercept(ActionInvocation action) throws Exception {
		String result=null;
		JsonResult<String> json=new JsonResult<String>();
		try{
			result=action.invoke();
		}catch (final Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage(e.getMessage());
			AbstractBaseAction	a=(AbstractBaseAction) action.getAction();
			a.outJson(json);
		}
		return result;
	}
//	public void sendMail(final Exception exception) {
//		Thread sender = new Thread() {
//			public void run() {
//				try {
//					JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//					mailSender.setHost("localhost");
//					mailSender.setUsername("xcbggb20130606@163.com");
//					mailSender.setPassword("123456");
//					Properties property = new Properties();
//					property.setProperty("mail.smtp.auth", "true");
//					mailSender.setJavaMailProperties(property);
//					MimeMessage mine = mailSender.createMimeMessage();
//					MimeMessageHelper helper = new MimeMessageHelper(mine,true, "utf-8");
//					helper.setFrom("xcbggb20130606@163.com");
//					helper.setTo("xcbggb20130606@163.com");
//					helper.setSubject("Email Excetion");
//					StringBuilder text = new StringBuilder();
//					text.append("<html><head></head><body><p>Dear Admin</p><br/>");
//					text.append("<p>Excetpion:</p>");
//					text.append(exceptionToHTML(exception));
//					text.append("<p></p><br/>");
//					text.append("<p>Thank you.</p><br/>");
//					text.append("<p>Postmaster</p><br/><p>&nbsp;</p><br/></body></html>");
//					helper.setText(text.toString(), true);
//					mailSender.send(mine);
//					logger.debug("Mail Sended.");
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		};
//		sender.start();
//	}
	/**
	 * parse exception object to xml format.
	 * 
	 * @param throwable
	 * @return
	 */
//	private String exceptionToHTML(Throwable throwable) {
//		StringBuilder buffer = new StringBuilder();
//		buffer.append("<div style='background:#fcfcfc;border solid 1px #cccccc;font-size:13px;color:red'>");
//		String msg=throwable.getMessage();
//		buffer.append("<div>" + msg+ "</div>");
//		buffer.append("<ul>");
//		StackTraceElement[] elements = throwable.getStackTrace();
//		for (StackTraceElement e : elements) {
//			buffer.append("<li>\tat " + e.getClassName() +"."+ e.getMethodName()
//					+ "(<u>" + e.getFileName() + ":" + e.getLineNumber()
//					+ "</u>)</li>");
//		}
//		buffer.append("</ul>");
//		Throwable cause = throwable.getCause();
//		if (cause != null) {
//			buffer.append(getCausedBy(cause));
//		}
//		buffer.append("</div>");
//		return buffer.toString();
//	}

	/**
	 * Get exception caused by.
	 * 
	 * @param cause
	 * @return
	 */
//	private String getCausedBy(Throwable cause) {
//		StringBuilder buffer = new StringBuilder();
//		StackTraceElement[] elements = cause.getStackTrace();
//		buffer.append("<p>Caused by:" + cause.getMessage() + "</p>");
//		buffer.append("<ul >");
//		for (StackTraceElement e : elements) {
//			buffer.append("<li>\tat " + e.getClassName() + e.getMethodName()
//					+ "(<u>" + e.getFileName() + ":" + e.getLineNumber()
//					+ "</u>)</li>");
//		}
//		buffer.append("</ul>");
//		Throwable a = cause.getCause();
//		if (a != null) {
//			buffer.append(getCausedBy(a));
//		}
//		return buffer.toString();
//	}
	 
	
}